import 'package:flutter/material.dart';
import '../../../core/platform/platform_detector.dart';

/// 响应式数据表格组件
class ResponsiveDataTable extends StatelessWidget {
  final List<DataColumn> columns;
  final List<DataRow> rows;
  final String? title;
  final VoidCallback? onRefresh;

  const ResponsiveDataTable({
    super.key,
    required this.columns,
    required this.rows,
    this.title,
    this.onRefresh,
  });

  @override
  Widget build(BuildContext context) {
    // 桌面平台使用完整表格
    if (PlatformDetector.isDesktop() || PlatformDetector.isWeb()) {
      return _buildDesktopTable(context);
    } else {
      // 移动平台使用卡片列表
      return _buildMobileList(context);
    }
  }

  /// 构建桌面表格
  Widget _buildDesktopTable(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (title != null || onRefresh != null) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (title != null)
                Text(
                  title!,
                  style: Theme.of(context).textTheme.titleLarge,
                ),
              if (onRefresh != null)
                IconButton(
                  icon: const Icon(Icons.refresh),
                  onPressed: onRefresh,
                ),
            ],
          ),
          const SizedBox(height: 16),
        ],
        Expanded(
          child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: DataTable(
              columns: columns,
              rows: rows,
              headingRowColor: WidgetStateProperty.all(
                Theme.of(context).primaryColor.withValues(alpha: 0.1),
              ),
              dataRowColor: WidgetStateProperty.all(
                Theme.of(context).cardColor,
              ),
              headingTextStyle: TextStyle(
                fontWeight: FontWeight.bold,
                color: Theme.of(context).primaryColor,
              ),
              dividerThickness: 0.5,
            ),
          ),
        ),
      ],
    );
  }

  /// 构建移动设备列表
  Widget _buildMobileList(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (title != null || onRefresh != null) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (title != null)
                Text(
                  title!,
                  style: Theme.of(context).textTheme.titleMedium?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                ),
              if (onRefresh != null)
                IconButton(
                  icon: const Icon(Icons.refresh),
                  onPressed: onRefresh,
                ),
            ],
          ),
          const SizedBox(height: 16),
        ],
        Expanded(
          child: ListView.separated(
            itemCount: rows.length,
            separatorBuilder: (context, index) => const SizedBox(height: 8),
            itemBuilder: (context, index) {
              return _buildMobileCard(context, index);
            },
          ),
        ),
      ],
    );
  }

  /// 构建移动设备卡片
  Widget _buildMobileCard(BuildContext context, int rowIndex) {
    final row = rows[rowIndex];
    final cells = row.cells;

    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: List.generate(columns.length, (index) {
            final column = columns[index];
            final cell = cells[index];

            return Padding(
              padding: const EdgeInsets.only(bottom: 8),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: 80,
                    child: Text(
                      _getColumnTitle(column),
                      style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 12,
                      ),
                    ),
                  ),
                  const SizedBox(width: 8),
                  Expanded(
                    child: DefaultTextStyle(
                      style: const TextStyle(
                        fontSize: 12,
                      ),
                      child: cell.child,
                    ),
                  ),
                ],
              ),
            );
          }),
        ),
      ),
    );
  }

  /// 获取列标题文本
  String _getColumnTitle(DataColumn column) {
    if (column.label is Text) {
      return (column.label as Text).data ?? '';
    }
    return '';
  }
}